package com.wxapp.custom.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.custom.api.pojo.entity.CustomAddressDO;
import com.wxapp.custom.api.pojo.vm.address.PAddressDetailVm;
import com.wxapp.custom.api.pojo.vm.address.PAddressPageVm;
import com.wxapp.custom.api.pojo.vo.address.QAddressAddVo;
import com.wxapp.custom.api.pojo.vo.address.QAddressUpdateVo;
import com.wxapp.custom.service.mapper.AddressMapper;
import com.wxapp.custom.service.service.AddressService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/10/9 14:13
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class AddressServiceImpl extends IBaseService implements AddressService {
    private final AddressMapper addressMapper;

    @Override
    @GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResultVm<String> add(UserVo userVo, QAddressAddVo qAddressAddVo) {
        var address = CopyUtil.copyObjectNew(qAddressAddVo, CustomAddressDO::new, (s, r) -> {
            r.setCreateBy(userVo.getId());
            r.setCustomId(userVo.getCustomId());
            return null;
        });
        if (qAddressAddVo.getDefaulted()){
            changeDefault();
        }
        addressMapper.insert(address);
        return success("保存地址成功");
    }

    @Override
    @GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResultVm<String> update(QAddressUpdateVo qAddressUpdateVo, UserVo userVo) {
        var address = addressMapper.selectById(qAddressUpdateVo.getId());
        if (CheckUtil.isNullObject(address)) {
            return error("没有找到地址信息");
        }
        CopyUtil.copyObject(qAddressUpdateVo, address, (s, r) -> {
            r.setUpdateBy(userVo.getId());
            return null;
        });
        if (qAddressUpdateVo.getDefaulted()){
            changeDefault();
        }
        addressMapper.updateById(address);
        return success("更新地址成功");
    }

    @Override
    public ResultVm<String> delete(String id, UserVo userVo) {
        var address = addressMapper.selectById(id);
        if (CheckUtil.isNullObject(address)) {
            return error("没有找到地址信息");
        }
        address.setStatus(false);
        addressMapper.updateById(address);
        return success("删除地址成功");
    }

    @Override
    public ResultVm<PAddressDetailVm> getDetail(String id) {
        var address = addressMapper.selectById(id);
        if (CheckUtil.isNullObject(address)) {
            return error("没有找到地址信息");
        }
        return success(CopyUtil.copyObjectNew(address, PAddressDetailVm::new, null));
    }

    @Override
    public ResultVm<List<PAddressPageVm>> list(UserVo userVo) {
        LambdaQueryWrapper<CustomAddressDO> query = new LambdaQueryWrapper<>();
        query.eq(CustomAddressDO::getCustomId, userVo.getCustomId())
                .eq(CustomAddressDO::getStatus,true);
        var selectPage = addressMapper.selectList(query);
        return success(CopyUtil.copyListNew(selectPage, PAddressPageVm::new, null));
    }

    private void changeDefault() {
        LambdaQueryWrapper<CustomAddressDO> query = new LambdaQueryWrapper<>();
        query.eq(CustomAddressDO::getDefaulted, true);
        var customAddressDO = addressMapper.selectFirst(query);
        if (CheckUtil.isNotNullObject(customAddressDO)) {
            customAddressDO.setDefaulted(false);
            addressMapper.updateById(customAddressDO);
        }
    }
}
